package com.sinosoft.cloud.access.abc.service;


import com.sinosoft.cloud.access.annotations.AccessService;
import com.sinosoft.cloud.access.net.AccessControl;
import com.sinosoft.cloud.access.service.AccessServiceBean;
import com.sinosoft.cloud.access.util.TimeUtil;
import com.sinosoft.cloud.cbs.fin.client.TempPayDataStoreService;
import com.sinosoft.cloud.cbs.fin.client.TempPayService;
import com.sinosoft.cloud.nb.app.client.NBPrintService;
import com.sinosoft.cloud.nb.app.client.UpDateContnoService;
import com.sinosoft.cloud.rest.TradeInfo;
import feign.RetryableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Description:1004新单缴费业务处理类
 * @Author:zhaoshulei
 * @Date:2017/9/19
 */

@AccessService(name = "abc", transCode = "1004")
public class NewContPayService extends AccessServiceBean {
    /**
     * 日志管理
     */
    private final Log cLogger = LogFactory.getLog(this.getClass());
    //需要调用的微服务
    /**
     * 更新保单号
     */
    @Autowired
    UpDateContnoService upDateContnoService;

    /**
     * 新单缴费
     */
    @Autowired
    TempPayService tempPayService;
    /**
     * 保单打印
     */
    @Autowired
    NBPrintService nbPrintService;

    /**
     * 财务落库
     */
    @Autowired
    TempPayDataStoreService tempPayDataStoreService;

    @Autowired
    AccessControl accessControl;

    @Override
    public TradeInfo service(TradeInfo msg) {

        //错误信息
        String errorStr = "";
        String errorLog = "";
        long microServiceStart = 0L;
        long microServiceEnd = 0L;
        /**
         * 调用微服务接口
         */
        TradeInfo respTradeInfo = msg;
        if ("on".equals(accessControl.getUpdateContNoFlag())){
            try {
                cLogger.info("开始调用更新保单号微服务！");
                microServiceStart = System.currentTimeMillis();
                respTradeInfo = upDateContnoService.service(respTradeInfo);
                microServiceEnd = System.currentTimeMillis();
                cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "更新保单号微服务"));
            } catch (RetryableException e) {
                microServiceEnd = System.currentTimeMillis();
                errorStr = getExceptionStr(microServiceStart, microServiceEnd, "更新保单号", e);
                return getErrorTradeInfo(errorStr);
            } catch (Exception e) {
                errorStr = getExceptionStr(0L, 0L, "更新保单号", e);
                return getErrorTradeInfo(errorStr);
            }
            if (respTradeInfo.hasError()) {
                errorStr = respTradeInfo.getErrorList().get(0).toString();
                errorLog = new StringBuffer().append("调用更新保单号微服务失败,原因是：").append(respTradeInfo.getErrorList().toString()).toString();
                return getCheckRuleFailInfo(errorStr, errorLog);
            } else {
                cLogger.info("调用更新保单号微服务成功！返回结果是：" + respTradeInfo.toString());
            }
        }
        try {
            cLogger.info("1.开始调用新单缴费微服务！");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = tempPayService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "新单缴费微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "新单缴费", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "新单缴费", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.hasError()) {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer().append("调用新单缴费微服务失败,原因是：").append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        } else {
            cLogger.info("调用新单缴费微服务成功！返回结果是：" + respTradeInfo.toString());
        }
        try {
            cLogger.info("2.开始调用保单打印微服务！");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = nbPrintService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "保单打印微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "保单打印", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "保单打印", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.hasError()) {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer().append("调用保单打印微服务失败,原因是：").append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr,errorLog);
        } else {
            cLogger.info("调用保单打印微服务成功！返回结果是：" + respTradeInfo.toString());
        }
        try {
            cLogger.info("3.开始新单缴费落库微服务！");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = tempPayDataStoreService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "新单缴费落库微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "新单缴费落库", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "新单缴费落库", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.hasError()) {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer().append("调用新单缴费落库微服务失败,原因是：").append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr,errorLog);
        } else {
            cLogger.info("调用新单缴费落库微服务成功！返回结果是：" + respTradeInfo.toString());
        }
        return respTradeInfo;
    }
}